package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.MapUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.supermarket.biz.mapper.GsNoticeMapper;
import com.sdy.supermarket.biz.model.GsNotice;
import com.sdy.supermarket.biz.service.GsNoticeService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import com.sdy.supermarket.biz.service.GsUserNoticeService;
import com.sdy.usercenter.api.UserQueryApi;
import com.sdy.usercenter.api.dto.ResourceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 韩亚恒
 * @since 2019-07-23
 */
@Slf4j
@Service
public class GsNoticeServiceImpl extends BaseServiceImpl<GsNotice> implements GsNoticeService {
    @Resource
    private GsNoticeMapper gsNoticeMapper;

    @Resource
    private UserQueryApi userQueryApi;

    @Autowired
    private GsUserNoticeService gsUserNoticeService;

    @Override
    public IPage<GsNotice> pageListGsNotice(String noticeName, String noticeText, Integer noticeStatus, Page<GsNotice> page) {
        LambdaQueryWrapper<GsNotice> lambda = new LambdaQueryWrapper();
        // 按照最后更新时间进行排序
        lambda.orderByDesc(GsNotice::getLastUpdateTime);
        if (StringUtil.isNotEmpty(noticeName) && StringUtil.isNotBlank(noticeName)) {
            lambda.like(GsNotice::getNoticeName, noticeName);
        }
        if (StringUtil.isNotEmpty(noticeText) && StringUtil.isNotBlank(noticeText)) {
            lambda.like(GsNotice::getNoticeContentText, noticeText);
        }
        if (noticeStatus != null) {
            lambda.eq(GsNotice::getNoticeStatus, noticeStatus);
        }
        IPage<GsNotice> iPage = page(page, lambda);
        // 根据存放的ID对比应用列表的ID来放入对应的应用名称
        iPage.setRecords(setPlatformName(iPage.getRecords()));
        return iPage;
    }

    @Override
    public List<ResourceDTO> getAppList() {
        List<ResourceDTO> resourceDTOList = userQueryApi.listApp();
        return resourceDTOList;
    }

    private List<GsNotice> setPlatformName(List<GsNotice> noticeList) {
        List<ResourceDTO> listApp = userQueryApi.listApp();
        listApp.forEach(app -> {
            noticeList.forEach(notice -> {
                if (notice.getNoticePlatformId() != null && notice.getNoticePlatformId().equals(app.getId())) {
                    notice.setNoticePlatformName(app.getName());
                }
            });
        });
        return noticeList;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int saveOrUpdateGsNotice(GsNotice gsNotice) throws BizException {
        boolean isAdd = false;
        if (gsNotice.getNoticeId() != null) {
            gsNotice.setCreateTime(null);
        } else {
            isAdd = true;
            gsNotice.setCreateTime(new Date());
        }
        gsNotice.setLastUpdateTime(new Date());
        Date now = new Date();
        if (now.before(gsNotice.getNoticeStartTime())) {
            gsNotice.setNoticeStatus(0);
        }
        if (now.after(gsNotice.getNoticeStartTime()) && now.before(gsNotice.getNoticeEndTime())) {
            gsNotice.setNoticeStatus(1);
        }
        if (now.after(gsNotice.getNoticeEndTime())) {
            gsNotice.setNoticeStatus(2);
        }
        boolean b = saveOrUpdate(gsNotice);
        if (isAdd) {
            GsNotice newAdd = gsNoticeMapper.selectOne(Wrappers.<GsNotice>lambdaQuery().eq(gsNotice.getNoticeName() != null, GsNotice::getNoticeName, gsNotice.getNoticeName())
                    .eq(gsNotice.getNoticeContentText() != null, GsNotice::getNoticeContentText, gsNotice.getNoticeContentText()));
            int addAllUserUnReadNotice = gsUserNoticeService.addAllUserUnReadNotice(newAdd.getNoticeId(), null);
            Assert.isTrue(!b || addAllUserUnReadNotice == 0, "新增失败");
            return 1;
        }
        return b ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteGsNoticeById(Integer noticeId) throws BizException {
        Assert.isNull(noticeId, "Id为null");
        int deleteById = gsNoticeMapper.deleteById(noticeId);
        return deleteById;
    }
}
